home *** CD-ROM | disk | FTP | other *** search
/ Super Shareware Collection / Super Shareware Collection.iso / os_2 / clisp.zip / DEFSTRUC.LSP < prev    next >
Lisp/Scheme  |  1994-02-05  |  35KB  |  762 lines

  1. ; Sources für DEFSTRUCT Macro.
  2. ; Bruno Haible 13.04.1988, 22.08.1988
  3. ; umgeschrieben am 02.09.1989 von Bruno Haible
  4.  
  5. (in-package "SYSTEM")
  6.  
  7. (defsetf %structure-ref %structure-store)
  8.  
  9. #| Erklärung der auftretenden Datentypen:
  10.  
  11.    (get name 'DEFSTRUCT-DESCRIPTION) =
  12.      #(names type keyword-constructor slotlist defaultfun0 defaultfun1 ...)
  13.  
  14.    names ist eine Codierung der INCLUDE-Verschachtelung für Structure name:
  15.    names = (name_1 ... name_i-1 name_i) wobei name=name_1,
  16.      name_1 enthält name_2, ..., name_i-1 enthält name_i.
  17.  
  18.    type (wenn der Typ der ganzen Structure gemeint ist):
  19.       = T                      Abspeicherung als normale Structure
  20.       = LIST                   Abspeicherung als Liste
  21.       = VECTOR                 Abspeicherung als (simple-)Vector
  22.       = (VECTOR element-type)  Abspeicherung als Vector mit Element-Typ
  23.  
  24.    keyword-constructor = NIL oder der Name des Keyword-Constructor
  25.  
  26.    slotlist ist eine gepackte Beschreibung der einzelnen slots einer Structure:
  27.    slotlist = ({slot}*)
  28.    slot = (name offset default type readonly)
  29.    wobei name der Slotname ist,
  30.               (NIL für den Slot, in dem der Structure-Name steht)
  31.          default der Defaultwert ist:
  32.               entweder eine Konstante, die zum Defaultwert evaluiert,
  33.               oder eine Form (ein Symbol oder eine Liste (SVREF ...)), die
  34.               bei Auswertung in einem beliebigen Environment eine Funktion
  35.               liefert, die bei Aufruf den Defaultwert liefert.
  36.          type der deklarierte Type für diesen Slot ist,
  37.          readonly = NIL oder = T angibt, ob dieser Slot readonly ist, d.h.
  38.               nach dem Aufbau der Structure nicht mehr mit (setf ...)
  39.               verändert werden kann.
  40.    Bei type = T belegt der Structure-Name den Slot 0, wird aber nicht in der
  41.      slotlist aufgeführt, da zu seiner Initialisierung nichts zu tun ist.
  42.  
  43. |#
  44.  
  45.  
  46. #| (ds-symbol-or-error x) liefert eine Fehlermeldung, falls x kein Symbol ist.
  47. |#
  48. (defun ds-symbol-or-error (x)
  49.   (unless (symbolp x)
  50.     (error #+DEUTSCH "~S: Das ist kein Symbol: ~S"
  51.            #+ENGLISH "~S: this is not a symbol: ~S"
  52.            #+FRANCAIS "~S : Ceci n'est pas un symbole: ~S"
  53.            'defstruct x
  54. ) ) )
  55.  
  56. #| Hilfsfunktion für beide Konstruktoren:
  57.    (ds-arg-default arg slot)
  58.    liefert zu einem Argument arg (Teil einer Argumentliste) den Teil der
  59.    Argumentliste, der dieses Argument mit dem Default für slot bindet.
  60. |#
  61.  
  62. (defun ds-arg-default (arg slot)
  63.   (let ((default (third slot)))
  64.     ; Default ist entweder Konstante oder Funktion oder Symbol
  65.     (if (constantp default)
  66.       (if (null default) arg `(,arg ,default))
  67.       `(,arg (SYS::%FUNCALL ,default))
  68. ) ) )
  69.  
  70. #| Hilfsfunktion für beide Konstruktoren:
  71.    (ds-make-constructor-body type name names size slotlist)
  72.    liefert den Ausdruck, der eine Structure vom vorgegebenen Typ
  73.    kreiert und füllt.
  74. |#
  75. (defun ds-make-constructor-body (type name names size slotlist)
  76.   `(LET ((OBJECT
  77.            ,(cond ((eq type 'T) `(%MAKE-STRUCTURE ,names ,size))
  78.                   ((eq type 'LIST) `(MAKE-LIST ,size))
  79.                   ((consp type) `(MAKE-ARRAY ,size :ELEMENT-TYPE ',(second type)))
  80.                   (t `(MAKE-ARRAY ,size))
  81.             )
  82.         ))
  83.      ,@(mapcar
  84.          #'(lambda (slot &aux (offset (second slot)))
  85.              `(SETF
  86.                 ,(cond ((eq type 'T)
  87.                         `(%STRUCTURE-REF ',name OBJECT ,offset) )
  88.                        ((eq type 'LIST)
  89.                         `(NTH ,offset OBJECT) )
  90.                        ((eq type 'VECTOR)
  91.                         `(SVREF OBJECT ,offset) )
  92.                        (t `(AREF OBJECT ,offset) )
  93.                  )
  94.                 ,(if (first slot)
  95.                    `(THE ,(fourth slot) ,(first slot))
  96.                    `(QUOTE ,(third slot))
  97.               )  )
  98.            )
  99.          slotlist
  100.        )
  101.      OBJECT
  102. )  )
  103.  
  104. #| Hilfsfunktion für ds-make-boa-constructor:
  105.  
  106.    (ds-arg-with-default arg slotlist)
  107.    liefert zu einem Argument arg (Teil einer Argumentliste) den Teil der
  108.    Argumentliste, der dieses Argument mit dem richtigen Defaultwert bindet.
  109. |#
  110.  
  111. (defun ds-arg-with-default (arg slotlist)
  112.   (if (listp arg)
  113.     ; Defaultwert ist bereits mitgegeben
  114.     arg
  115.     ; nur ein Symbol
  116.     (let ((slot (find arg slotlist :key #'first :test #'eq)))
  117.       (if slot
  118.         ; Slot gefunden -> dessen Defaultwert nehmen
  119.         (ds-arg-default arg slot)
  120.         ; Slot nicht gefunden, kein Defaultwert
  121.         arg
  122. ) ) ) )
  123.  
  124. #| (ds-make-boa-constructor descriptor type name names size slotlist)
  125.    liefert die Form, die den BOA-Konstrukor definiert.
  126. |#
  127. (defun ds-make-boa-constructor (descriptor type name names size slotlist)
  128.   (let ((constructorname (first descriptor))
  129.         (arglist (second descriptor)))
  130.     ; auf &KEY und &ALLOW-OTHER-KEYS testen:
  131.     (let ((keying (or (member '&KEY arglist :test #'eq)
  132.                       (member '&ALLOW-OTHER-KEYS arglist :test #'eq)
  133.          ))       )
  134.       (when keying
  135.         (error #+DEUTSCH "~S ~S: Die Argumentliste für eine keywordfreie Konstruktorfunktion ~S darf kein ~S enthalten: ~S"
  136.                #+ENGLISH "~S ~S: the argument list for the BOA contructor ~S must not contain ~S: ~S"
  137.                #+FRANCAIS "~S ~S : La liste d'arguments pour un constructeur ~S libre de mot-clés ne peux pas contenir ~S: ~S"
  138.                'defstruct name constructorname (car keying) arglist
  139.     ) ) )
  140.     ; angegebene Argumente sammeln:
  141.     (let* ((argnames
  142.              (let ((L nil))
  143.                (dolist (arg arglist)
  144.                  (unless (member arg lambda-list-keywords :test #'eq)
  145.                    (push (if (listp arg) (first arg) arg) L)
  146.                ) )
  147.                (nreverse L)
  148.            ) )
  149.            ; argnames ist die Liste aller bereits in der Paramterliste mit
  150.            ; Werten versehenen Argumente.
  151.            (new-arglist ; neue Argumentliste
  152.              `(; required args:
  153.                ,@(do ((arglistr arglist (cdr arglistr))
  154.                       (arg)
  155.                       (required-args nil))
  156.                      ((or (endp arglistr)
  157.                           (member (setq arg (car arglistr)) lambda-list-keywords :test #'eq)
  158.                       )
  159.                       (nreverse required-args)
  160.                      )
  161.                    (push arg required-args)
  162.                  )
  163.                ; optional args:
  164.                ,@(do ((arglistr (cdr (member '&optional arglist :test #'eq)) (cdr arglistr))
  165.                       (arg)
  166.                       (optionals nil))
  167.                      ((or (endp arglistr)
  168.                           (member (setq arg (car arglistr)) lambda-list-keywords :test #'eq)
  169.                       )
  170.                       (if (null optionals) nil (cons '&optional (nreverse optionals)))
  171.                      )
  172.                    (push (ds-arg-with-default arg slotlist) optionals)
  173.                  )
  174.                ; rest arg:
  175.                ,@(let ((arglistr (member '&rest arglist :test #'eq)))
  176.                    (if arglistr `(&rest ,(second arglistr)) '())
  177.                  )
  178.                ; aux args:
  179.                &aux
  180.                ,@(do ((aux-args-r (cdr (member '&aux arglist :test #'eq)) (cdr aux-args-r))
  181.                       (aux-arg)
  182.                       (new-aux-args nil))
  183.                      ((or (null aux-args-r)
  184.                           (member (setq aux-arg (car aux-args-r)) lambda-list-keywords :test #'eq)
  185.                       )
  186.                       (nreverse new-aux-args)
  187.                      )
  188.                    (push (ds-arg-with-default aux-arg slotlist) new-aux-args)
  189.                  )
  190.                ,@(let ((slotinitlist nil))
  191.                    (dolist (slot slotlist)
  192.                      (when (first slot)
  193.                        (unless (member (first slot) argnames :test #'eq)
  194.                          (push (ds-arg-with-default (first slot) slotlist) slotinitlist)
  195.                    ) ) )
  196.                    (nreverse slotinitlist)
  197.               )  )
  198.           ))
  199.       `(DEFUN ,constructorname ,new-arglist
  200.          ,(ds-make-constructor-body type name names size slotlist)
  201.        )
  202. ) ) )
  203.  
  204. #| (ds-make-keyword-constructor descriptor type name names size slotlist)
  205.    liefert die Form, die den Keyword-Konstruktor definiert.
  206. |#
  207. (defun ds-make-keyword-constructor (descriptor type name names size slotlist)
  208.   `(DEFUN ,descriptor
  209.      (&KEY
  210.       ,@(mapcap
  211.           #'(lambda (slot)
  212.               (if (first slot) (list (ds-arg-default (first slot) slot)) '())
  213.             )
  214.           slotlist
  215.      )  )
  216.      ,(ds-make-constructor-body type name names size slotlist)
  217. )  )
  218.  
  219. #| (ds-make-pred predname type name name-offset)
  220.    liefert die Form, die das Typtestprädikat für die Structure name kreiert.
  221.    Dabei ist:
  222.    type         der Typ der Structure,
  223.    name         der Name der Structure,
  224.    predname     der Name des Typtestprädikats,
  225.    name-offset  (nur bei type /= T maßgeblich)
  226.                 die Stelle, an der der Name abgespeichert wird.
  227. |#
  228. (defun ds-make-pred (predname type name name-offset)
  229.   `(,@(if (eq type 'T) `((PROCLAIM '(INLINE ,predname))) '())
  230.     (DEFUN ,predname (OBJECT)
  231.       ,(if (eq type 'T)
  232.          `(%STRUCTURE-TYPE-P ',name OBJECT)
  233.          (if (eq type 'LIST)
  234.            `(AND (CONSP OBJECT)
  235.                  ,@(if (eql name-offset 0)
  236.                      `((EQ (CAR OBJECT) ',name))
  237.                      `((> (LENGTH OBJECT) ,name-offset)
  238.                        (EQ (NTH ,name-offset OBJECT) ',name)
  239.                       )
  240.             )      )
  241.            `(AND (SIMPLE-VECTOR-P OBJECT)
  242.                  (> (LENGTH OBJECT) ,name-offset)
  243.                  (EQ (SVREF OBJECT ,name-offset) ',name)
  244.             )
  245.        ) )
  246.    ))
  247. )
  248.  
  249. (defun ds-make-copier (copiername name type)
  250.   (declare (ignore name))
  251.   `(,@(if (or (eq type 'T) (eq type 'LIST))
  252.         `((PROCLAIM '(INLINE ,copiername)))
  253.         '()
  254.       )
  255.     (DEFUN ,copiername (STRUCTURE)
  256.       ,(if (eq type 'T)
  257.          '(%COPY-STRUCTURE STRUCTURE)
  258.          (if (eq type 'LIST)
  259.            '(COPY-LIST STRUCTURE)
  260.            (if (consp type)
  261.              `(LET* ((OBJ-LENGTH (ARRAY-TOTAL-SIZE STRUCTURE))
  262.                      (OBJECT (MAKE-ARRAY OBJ-LENGTH :ELEMENT-TYPE (QUOTE ,(second type))))
  263.                     )
  264.                 (DOTIMES (I OBJ-LENGTH OBJECT)
  265.                   (SETF (AREF OBJECT I) (AREF STRUCTURE I))
  266.               ) )
  267.              `(LET* ((OBJ-LENGTH (LENGTH STRUCTURE))
  268.                      (OBJECT (MAKE-ARRAY OBJ-LENGTH)))
  269.                 (DOTIMES (I OBJ-LENGTH OBJECT)
  270.                    (SETF (SVREF OBJECT I) (SVREF STRUCTURE I))
  271.               ) )
  272.        ) ) )
  273. )  ))
  274.  
  275. (defun ds-make-accessors (name type concname slotlist)
  276.   (mapcap
  277.     #'(lambda (slot)
  278.         (if (first slot)
  279.           (let ((accessorname (concat-pnames concname (first slot)))
  280.                 (offset (second slot))
  281.                 (slottype (fourth slot)))
  282.             `((PROCLAIM '(FUNCTION ,accessorname (,name) ,slottype))
  283.               (PROCLAIM '(INLINE ,accessorname))
  284.               (DEFUN ,accessorname (OBJECT)
  285.                 (THE ,slottype
  286.                   ,(if (eq type 'T)
  287.                      `(%STRUCTURE-REF ',name OBJECT ,offset)
  288.                      (if (eq type 'LIST)
  289.                        `(NTH ,offset OBJECT)
  290.                        (if (consp type)
  291.                          `(AREF OBJECT ,offset)
  292.                          `(SVREF OBJECT ,offset)
  293.              )) )  ) ) )
  294.           )
  295.           '()
  296.       ) )
  297.     slotlist
  298. ) )
  299.  
  300. (defun ds-make-defsetfs (name type concname slotlist)
  301.   (mapcap
  302.     #'(lambda (slot)
  303.         (if (and (first slot) (not (fifth slot))) ; not READ-ONLY
  304.           (let ((accessorname (concat-pnames concname (first slot)))
  305.                 (offset (second slot))
  306.                 (slottype (fourth slot)))
  307.             `((DEFSETF ,accessorname (STRUCT) (VALUE)
  308.                 ,(if (eq type 'T)
  309.                    `(LIST '%STRUCTURE-STORE '',name
  310.                       STRUCT
  311.                       ,offset
  312.                       ,(if (eq 'T slottype)
  313.                          `VALUE
  314.                          `(LIST 'THE ',slottype VALUE)
  315.                     )  )
  316.                    (if (eq type 'LIST)
  317.                      `(LIST 'SETF (LIST 'NTH ,offset STRUCT) VALUE)
  318.                      (if (consp type)
  319.                        `(LIST 'SETF (LIST 'AREF STRUCT ,offset) VALUE)
  320.                        `(LIST 'SETF (LIST 'SVREF STRUCT ,offset) VALUE)
  321.              ))  ) ) )
  322.       ) ) )
  323.     slotlist
  324. ) )
  325.  
  326. ; Ein Hook für CLOS
  327. (defun clos::define-structure-class (name) (declare (ignore name)) ) ; vorläufig
  328.  
  329. (defmacro defstruct (name-and-options . docstring-and-slotargs)
  330.   (let ((name                              name-and-options)
  331.         (options                           nil)
  332.         (conc-name-option                  t)
  333.         (constructor-option-list           nil)
  334.         (keyword-constructor               nil)
  335.         (copier-option                     t)
  336.         (predicate-option                  0)
  337.         (include-option                    nil)
  338.          names
  339.          namesform
  340.         (namesbinding                      nil)
  341.         (print-function-option             nil)
  342.         (type-option                       t)
  343.         (named-option                      0)
  344.         (initial-offset-option             0)
  345.         (initial-offset                    0)
  346.         (docstring                         nil)
  347.         (slotargs                          docstring-and-slotargs)
  348.          size
  349.         (include-skip                      0)
  350.         (inherited-slot-count              0)
  351.         (slotlist                          nil)
  352.         (slotdefaultvars                   nil)
  353.         (slotdefaultfuns                   nil)
  354.          constructor-forms                      )
  355.     ;; name-and-options überprüfen:
  356.     (when (listp name-and-options)
  357.       (setq name (first name-and-options))
  358.       (setq options (rest name-and-options))
  359.     ) ; andernfalls sind name und options schon korrekt.
  360.     (unless (and (symbolp name) (not (keywordp name)))
  361.       (error #+DEUTSCH "~S: Falsche Syntax für Name und Optionen: ~S"
  362.              #+ENGLISH "~S: invalid syntax for name and options: ~S"
  363.              #+FRANCAIS "~S : Mauvaise syntaxe pour un nom et des options: ~S"
  364.              'defstruct name-and-options
  365.     ) )
  366.     ; name ist ein Symbol, options die Liste der Optionen.
  367.     ;; Abarbeitung der Optionen:
  368.     (dolist (option options)
  369.       (when (keywordp option) (setq option (list option))) ; Option ohne Argumente
  370.       (if (listp option)
  371.         (if (keywordp (car option))
  372.           (case (first option)
  373.             (:CONC-NAME
  374.                (setq conc-name-option (or (second option) ""))
  375.             )
  376.             (:CONSTRUCTOR
  377.                (if (atom (cdr option))
  378.                  ; Default-Keyword-Constructor
  379.                  (push (concat-pnames "MAKE-" name) constructor-option-list)
  380.                  (let ((arg (second option)))
  381.                    (ds-symbol-or-error arg)
  382.                    (push
  383.                      (if (atom (cddr option))
  384.                        arg ; Keyword-Constructor
  385.                        (if (not (listp (third option)))
  386.                          (error #+DEUTSCH "~S ~S: Argumentliste muß eine Liste sein: ~S"
  387.                                 #+ENGLISH "~S ~S: argument list should be a list: ~S"
  388.                                 #+FRANCAIS "~S ~S : La liste d'arguments doit être une liste: ~S"
  389.                                 'defstruct name (third option)
  390.                          )
  391.                          (rest option) ; BOA-Constructor
  392.                      ) )
  393.                      constructor-option-list
  394.             )  ) ) )
  395.             (:COPIER
  396.                (when (consp (cdr option))
  397.                  (let ((arg (second option)))
  398.                    (ds-symbol-or-error arg)
  399.                    (setq copier-option arg)
  400.             )  ) )
  401.             (:PREDICATE
  402.                (when (consp (cdr option))
  403.                  (let ((arg (second option)))
  404.                    (ds-symbol-or-error arg)
  405.                    (setq predicate-option arg)
  406.             )  ) )
  407.             ((:INCLUDE :INHERIT)
  408.                (if (null include-option)
  409.                  (setq include-option option)
  410.                  (error #+DEUTSCH "~S ~S: Es darf nur ein :INCLUDE-Argument geben: ~S"
  411.                         #+ENGLISH "~S ~S: At most one :INCLUDE argument may be specified: ~S"
  412.                         #+FRANCAIS "~S ~S : Il ne peut y avoir qu'un argument :INCLUDE: ~S"
  413.                         'defstruct name options
  414.             )  ) )
  415.             (:PRINT-FUNCTION
  416.                (let ((arg (second option)))
  417.                  (when (and (consp arg) (eq (first arg) 'FUNCTION))
  418.                    (warn #+DEUTSCH "~S: Bei :PRINT-FUNCTION ist FUNCTION bereits implizit.~@
  419.                                     Verwende daher ~S statt ~S."
  420.                          #+ENGLISH "~S: Use of :PRINT-FUNCTION implicitly applies FUNCTION.~@
  421.                                     Therefore using ~S instead of ~S."
  422.                          #+FRANCAIS "~S : FUNCTION est déjà implicite avec :PRINT-FUNCTION.~@
  423.                                      C'est pourquoi ~S est utilisé au lieu de ~S."
  424.                          'defstruct (second arg) arg
  425.                    )
  426.                    (setq arg (second arg))
  427.                  )
  428.                  (setq print-function-option
  429.                    (if (symbolp arg)
  430.                      ; ein Ausdruck, der eine eventuelle lokale Definition
  431.                      ; von arg mitberücksichtigt, aber nicht erfordert:
  432.                      `(FUNCTION ,(concat-pnames name "-PRINT-FUNCTION")
  433.                         (LAMBDA (STRUCT STREAM DEPTH)
  434.                           (,arg STRUCT STREAM DEPTH)
  435.                       ) )
  436.                      `#',arg
  437.             )  ) ) )
  438.             (:TYPE (setq type-option (second option)))
  439.             (:NAMED (setq named-option t))
  440.             (:INITIAL-OFFSET (setq initial-offset-option (or (second option) 0)))
  441.             (T (error #+DEUTSCH "~S ~S: Die Option ~S gibt es nicht."
  442.                       #+ENGLISH "~S ~S: unknown option ~S"
  443.                       #+FRANCAIS "~S ~S : Option ~S non reconnue."
  444.                       'defstruct name (first option)
  445.           ) )  )
  446.           (error #+DEUTSCH "~S ~S: Falsche Syntax in ~S-Option: ~S"
  447.                  #+ENGLISH "~S ~S: invalid syntax in ~S option: ~S"
  448.                  #+FRANCAIS "~S ~S : Mauvaise syntaxe dans l'option ~S: ~S"
  449.                  'defstruct name 'defstruct option
  450.         ) )
  451.         (error #+DEUTSCH "~S ~S: Das ist keine ~S-Option: ~S"
  452.                #+ENGLISH "~S ~S: not a ~S option: ~S"
  453.                #+FRANCAIS "~S ~S : Ceci n'est pas une option ~S: ~S"
  454.                'defstruct name 'defstruct option
  455.     ) ) )
  456.     ; conc-name-option ist entweder T oder "" oder das :CONC-NAME-Argument.
  457.     ; constructor-option-list ist eine Liste aller :CONSTRUCTOR-Argumente,
  458.     ;   jeweils in der Form  symbol  oder  (symbol arglist . ...).
  459.     ; copier-option ist entweder T oder das :COPIER-Argument.
  460.     ; predicate-option ist entweder 0 oder das :PREDICATE-Argument.
  461.     ; include-option ist entweder NIL oder die gesamte
  462.     ;   :INCLUDE/:INHERIT-Option.
  463.     ; print-function-option ist NIL oder eine Form, die die Print-Function
  464.     ;   liefert.
  465.     ; type-option ist entweder T oder das :TYPE-Argument.
  466.     ; named-option ist entweder 0 oder T.
  467.     ; initial-offset-option ist entweder 0 oder das :INITIAL-OFFSET-Argument.
  468.     ;; Überprüfung der Optionen:
  469.     (setq named-option (or (eq type-option 'T) (eq named-option 'T)))
  470.     ; named-option (NIL oder T) gibt an, ob der Name in der Structure steckt.
  471.     (if named-option
  472.       (when (eql predicate-option 0)
  473.         (setq predicate-option (concat-pnames name "-P")) ; Defaultname
  474.       )
  475.       (unless (or (eql predicate-option 0) (eq predicate-option 'NIL))
  476.         (error #+DEUTSCH "~S ~S: Bei unbenannten Structures kann es kein :PREDICATE geben."
  477.                #+ENGLISH "~S ~S: There is no :PREDICATE on unnamed structures."
  478.                #+FRANCAIS "~S ~S : Il ne peut pas y avoir de :PREDICATE avec des structures anonymes."
  479.                'defstruct name
  480.     ) ) )
  481.     ; predicate-option ist
  482.     ;   bei named-option=T: entweder NIL oder der Name des Typtestprädikats,
  483.     ;   bei named-option=NIL bedeutungslos.
  484.     (if (eq conc-name-option 'T)
  485.       (setq conc-name-option (string-concat (string name) "-"))
  486.     )
  487.     ; conc-name-option ist der Namensprefix.
  488.     (if (null constructor-option-list)
  489.       (setq constructor-option-list (list (concat-pnames "MAKE-" name)))
  490.       (setq constructor-option-list (remove 'NIL constructor-option-list))
  491.     )
  492.     ; constructor-option-list ist eine Liste aller zu kreierenden Konstruktoren,
  493.     ;   jeweils in der Form  symbol  oder  (symbol arglist . ...).
  494.     (if (eq copier-option 'T)
  495.       (setq copier-option (concat-pnames "COPY-" name))
  496.     )
  497.     ; copier-option ist entweder NIL oder der Name der Kopierfunktion.
  498.     (unless (or (eq type-option 'T)
  499.                 (eq type-option 'VECTOR)
  500.                 (eq type-option 'LIST)
  501.                 (and (consp type-option) (eq (first type-option) 'VECTOR))
  502.             )
  503.       (error #+DEUTSCH "~S ~S: Unzulässige :TYPE-Option ~S"
  504.              #+ENGLISH "~S ~S: invalid :TYPE option ~S"
  505.              #+FRANCAIS "~S ~S : Option :TYPE inadmissible: ~S"
  506.              'defstruct name type-option
  507.     ) )
  508.     ; type-option ist entweder T oder LIST oder VECTOR oder (VECTOR ...)
  509.     (unless (and (integerp initial-offset-option) (>= initial-offset-option 0))
  510.       (error #+DEUTSCH "~S ~S: Der :INITIAL-OFFSET muß ein Integer >=0 sein, nicht ~S"
  511.              #+ENGLISH "~S ~S: The :INITIAL-OFFSET must be a nonnegative integer, not ~S"
  512.              #+FRANCAIS "~S ~S : :INITIAL-OFFSET doit être un entier positif ou zéro et non ~S"
  513.              'defstruct name initial-offset-option
  514.     ) )
  515.     ; initial-offset-option ist ein Integer >=0.
  516.     (when (and (plusp initial-offset-option) (eq type-option 'T))
  517.       (error #+DEUTSCH "~S ~S: :INITIAL-OFFSET darf nur zusammen mit :TYPE angegeben werden: ~S"
  518.              #+ENGLISH "~S ~S: :INITIAL-OFFSET must not be specified without :TYPE : ~S"
  519.              #+FRANCAIS "~S ~S : :INITIAL-OFFSET ne peut être précisé qu'ensemble avec :TYPE: ~S"
  520.              'defstruct name options
  521.     ) )
  522.     ; Bei type-option=T ist initial-offset-option=0.
  523.     (when (eq type-option 'T) (setq include-skip 1))
  524.     ; include-skip ist 1 bei type-option=T, 0 sonst.
  525.     (when (stringp (first docstring-and-slotargs))
  526.       (setq docstring (first docstring-and-slotargs))
  527.       (setq slotargs (rest docstring-and-slotargs))
  528.     ) ; sonst stimmen docstring und slotargs bereits.
  529.     ; docstring ist entweder NIL oder ein String.
  530.     ; slotargs sind die restlichen Argumente.
  531.     (if include-option
  532.       (let* ((option (rest include-option))
  533.              (subname (first option))
  534.              (incl-desc (get subname 'DEFSTRUCT-DESCRIPTION)))
  535.         (when (null incl-desc)
  536.           (error #+DEUTSCH "~S ~S: Teilstruktur ~S ist nicht definiert."
  537.                  #+ENGLISH "~S ~S: included structure ~S has not been defined."
  538.                  #+FRANCAIS "~S ~S : La structure incluse ~S n'est pas définie."
  539.                  'defstruct name subname
  540.         ) )
  541.         (setq names (cons name (svref incl-desc 0)))
  542.         (setq namesbinding
  543.               (list
  544.                 (list
  545.                   (setq namesform (gensym))
  546.                   `(CONS ',name (LOAD-TIME-VALUE (SVREF (GET ',subname 'DEFSTRUCT-DESCRIPTION) 0)))
  547.         )     ) )
  548.         (unless (equalp (svref incl-desc 1) type-option)
  549.           (error #+DEUTSCH "~S ~S: Teilstruktur ~S muß vom selben Typ ~S sein."
  550.                  #+ENGLISH "~S ~S: included structure ~S must be of the same type ~S."
  551.                  #+FRANCAIS "~S ~S : La structure incluse ~S doit être du même type ~S."
  552.                  'defstruct name subname type-option
  553.         ) )
  554.         (setq slotlist (nreverse (mapcar #'copy-list (svref incl-desc 3))))
  555.         ; slotlist ist die umgedrehte Liste der vererbten Slots
  556.         (when slotlist (setq include-skip (1+ (second (first slotlist)))))
  557.         ; include-skip >=0 ist die Anzahl der bereits von der Teilstruktur
  558.         ;   verbrauchten Slots, das "size" der Teilstruktur.
  559.         ; Weitere Argumente der :INCLUDE-Option abarbeiten:
  560.         (dolist (slotarg (rest option))
  561.           (let* ((slotname (if (atom slotarg) slotarg (first slotarg)))
  562.                  (slot (find slotname slotlist :key #'first :test #'eq)))
  563.             (when (null slot)
  564.               (error #+DEUTSCH "~S ~S: Teilstruktur ~S hat keine Komponente namens ~S."
  565.                      #+ENGLISH "~S ~S: included structure ~S has no component with name ~S."
  566.                      #+FRANCAIS "~S ~S : La structure incluse ~S n'a pas de composante de nom ~S."
  567.                      'defstruct name subname slotname
  568.             ) )
  569.             (if (atom slotarg)
  570.               (setf (third slot) 'NIL) ; Default auf NIL überschreiben
  571.               (progn
  572.                 (let ((default (second slotarg)))
  573.                   (unless (constantp default)
  574.                     (push
  575.                       `(FUNCTION ,(concat-pnames "DEFAULT-" slotname)
  576.                          (LAMBDA () ,default)
  577.                        )
  578.                       slotdefaultfuns
  579.                     )
  580.                     (setq default (gensym))
  581.                     (push default slotdefaultvars)
  582.                   )
  583.                   (setf (third slot) default)
  584.                 )
  585.                 ; slot-options dieses Slot-Specifier abarbeiten:
  586.                 (do ((slot-arglistr (cddr slotarg) (cddr slot-arglistr)))
  587.                     ((endp slot-arglistr))
  588.                   (let ((slot-keyword (first slot-arglistr))
  589.                         (slot-key-value (second slot-arglistr)))
  590.                     (cond ((eq slot-keyword ':READ-ONLY)
  591.                            (if slot-key-value
  592.                              (setf (fifth slot) t)
  593.                              (if (fifth slot)
  594.                                (error #+DEUTSCH "~S ~S: Der READ-ONLY-Slot ~S von Teilstruktur ~S muß auch in ~S READ-ONLY bleiben."
  595.                                       #+ENGLISH "~S ~S: The READ-ONLY slot ~S of the included structure ~S must remain READ-ONLY in ~S."
  596.                                       #+FRANCAIS "~S ~S : Le composant READ-ONLY ~S de la structure incluse ~S doit rester READ-ONLY dans ~S."
  597.                                       'defstruct name slotname subname name
  598.                                )
  599.                                (setf (fifth slot) nil)
  600.                           )) )
  601.                           ((eq slot-keyword ':TYPE)
  602.                            (unless (subtypep slot-key-value (fourth slot))
  603.                              (error #+DEUTSCH "~S ~S: Der Typ ~S von Slot ~S muß ein Untertyp des in Teilstruktur ~S definierten Typs ~S sein."
  604.                                     #+ENGLISH "~S ~S: The type ~S of slot ~S should be a subtype of the type defined for the included strucure ~S, namely ~S."
  605.                                     #+FRANCAIS "~S ~S : Le type ~S du composant ~S doit être un sous-type du type défini dans la structure incluse ~S, c'est-à-dire ~S."
  606.                                     'defstruct name slot-key-value slotname subname (fourth slot)
  607.                            ) )
  608.                            (setf (fourth slot) slot-key-value)
  609.                           )
  610.                           (t (error #+DEUTSCH "~S ~S: ~S ist keine Slot-Option."
  611.                                     #+ENGLISH "~S ~S: ~S is not a slot option."
  612.                                     #+FRANCAIS "~S ~S : ~S n'est pas un option de composant."
  613.                                     'defstruct name slot-keyword
  614.                           )  )
  615.                 ) ) )
  616.         ) ) ) )
  617.         (when (eq (first include-option) ':INHERIT)
  618.           (setq inherited-slot-count (length slotlist))
  619.       ) )
  620.       (progn
  621.         (setq names (list name))
  622.         (setq namesform `',names)
  623.     ) )
  624.     ; names ist die Include-Verschachtelung, namesform die Form dazu.
  625.     ; slotlist ist die bisherige Slotliste, umgedreht.
  626.     ; inherited-slot-count ist die Anzahl der Slots, die beim Bilden der
  627.     ; Accessoren zu ignorieren sind.
  628.     (when (and named-option ; benannte Structure
  629.                (consp type-option) ; vom Typ (VECTOR ...)
  630.                ; muß den/die Namen enthalten können:
  631.                (not (typep names (second type-option)))
  632.           )
  633.       (error #+DEUTSCH "~S ~S: Structure vom Typ ~S kann den Namen nicht enthalten."
  634.              #+ENGLISH "~S ~S: structure of type ~S can't hold the name."
  635.              #+FRANCAIS "~S ~S : Une structure de type ~S ne peut pas contenir le nom."
  636.              'defstruct name type-option
  637.     ) )
  638.     ; Aufbau der Structure:
  639.     ; names, evtl. include-Slots, initial-offset-option mal NIL, Slots.
  640.     ; Aufbau von Vektor oder Liste:
  641.     ; include-Anteil, initial-offset-option mal NIL, evtl. Name, Slots.
  642.     (setq initial-offset (+ include-skip initial-offset-option))
  643.     (unless (eq type-option 'T)
  644.       (when named-option
  645.         (push
  646.           (list nil ; Kennzeichen für Typerkennungs-Slot
  647.                 (setq initial-offset-option initial-offset)
  648.                 name ; "Defaultwert" = name
  649.                 'SYMBOL ; Typ = Symbol
  650.                 T) ; Read-Only
  651.           slotlist
  652.         )
  653.         (setq initial-offset (1+ initial-offset))
  654.     ) )
  655.     ; Die einzelnen Slots kommen ab initial-offset.
  656.     ; Bei type/=T (also Vektor oder Liste) und named-option sitzt
  657.     ;   der Name in Slot Nummer  initial-offset-option = (1- initial-offset).
  658.     ; Abarbeitung der einzelnen Slots:
  659.     (let ((offset initial-offset))
  660.       (dolist (slotarg slotargs)
  661.         (let (slotname
  662.               default)
  663.           (if (atom slotarg)
  664.             (setq slotname slotarg  default nil)
  665.             (setq slotname (first slotarg)  default (second slotarg))
  666.           )
  667.           (unless (constantp default)
  668.             (push
  669.               `(FUNCTION ,(concat-pnames "DEFAULT-" slotname)
  670.                  (LAMBDA () ,default)
  671.                )
  672.               slotdefaultfuns
  673.             )
  674.             (setq default (gensym))
  675.             (push default slotdefaultvars)
  676.           )
  677.           (when (find slotname slotlist :key #'first :test #'eq)
  678.             (error #+DEUTSCH "~S ~S: Es kann nicht mehrere Slots mit demselben Namen ~S geben."
  679.                    #+ENGLISH "~S ~S: There may be only one slot with the name ~S."
  680.                    #+FRANCAIS "~S ~S : Il ne peut pas y avoir plusieurs composants avec le même nom ~S."
  681.                    'defstruct name slotname
  682.           ) )
  683.           (let ((type t) (read-only nil))
  684.             (when (consp slotarg)
  685.               (do ((slot-arglistr (cddr slotarg) (cddr slot-arglistr)))
  686.                   ((endp slot-arglistr))
  687.                 (let ((slot-keyword (first slot-arglistr))
  688.                       (slot-key-value (second slot-arglistr)))
  689.                   (cond ((eq slot-keyword ':READ-ONLY)
  690.                          (setq read-only (if slot-key-value t nil))
  691.                         )
  692.                         ((eq slot-keyword ':TYPE) (setq type slot-key-value))
  693.                         (t (error #+DEUTSCH "~S ~S: ~S ist keine Slot-Option."
  694.                                   #+ENGLISH "~S ~S: ~S is not a slot option."
  695.                                   #+FRANCAIS "~S ~S : ~S n'est pas une option de composant."
  696.                                   'defstruct name slot-keyword
  697.                         )  )
  698.             ) ) ) )
  699.             (push (list slotname offset default type read-only) slotlist)
  700.         ) )
  701.         (incf offset)
  702.       )
  703.       (setq size offset)
  704.     )
  705.     ; size = Gesamtlänge der Structure
  706.     (setq slotlist (nreverse slotlist))
  707.     (setq slotdefaultfuns (nreverse slotdefaultfuns))
  708.     (setq slotdefaultvars (nreverse slotdefaultvars))
  709.     ; Die slots in slotlist sind jetzt wieder aufsteigend geordnet.
  710.     (setq constructor-forms
  711.       (mapcar
  712.         #'(lambda (constructor-option)
  713.             (if (consp constructor-option)
  714.               (ds-make-boa-constructor
  715.                 constructor-option type-option name namesform size slotlist
  716.               )
  717.               (progn
  718.                 (if (null keyword-constructor)
  719.                   (setq keyword-constructor constructor-option)
  720.                 )
  721.                 (ds-make-keyword-constructor
  722.                   constructor-option type-option name namesform size slotlist
  723.           ) ) ) )
  724.         constructor-option-list
  725.     ) )
  726.     ; constructor-forms = Liste der Formen, die die Konstruktoren definieren.
  727.     (let ((index 4))
  728.       (dolist (defaultvar slotdefaultvars)
  729.         (setf (third (find defaultvar slotlist :key #'third :test #'eq))
  730.               `(SVREF (GET ',name 'DEFSTRUCT-DESCRIPTION) ,index)
  731.         )
  732.         (incf index)
  733.     ) )
  734.     ; slotlist enthält nun keine der slotdefaultvars mehr.
  735.     `(EVAL-WHEN (LOAD COMPILE EVAL)
  736.        (LET ()
  737.          (LET ,(append namesbinding (mapcar #'list slotdefaultvars slotdefaultfuns))
  738.            ,@constructor-forms
  739.            (%PUT ',name 'DEFSTRUCT-DESCRIPTION
  740.                  (VECTOR ,namesform ',type-option ',keyword-constructor ',slotlist
  741.                          ,@slotdefaultvars
  742.          ) )     )
  743.          ,@(if (eq type-option 'T) `((CLOS::DEFINE-STRUCTURE-CLASS ',name)))
  744.          ,@(if (and named-option predicate-option)
  745.              (ds-make-pred predicate-option type-option name initial-offset-option)
  746.            )
  747.          ,@(if copier-option (ds-make-copier copier-option name type-option))
  748.          ,@(let ((directslotlist (nthcdr inherited-slot-count slotlist)))
  749.              `(,@(ds-make-accessors name type-option conc-name-option directslotlist)
  750.                ,@(ds-make-defsetfs name type-option conc-name-option directslotlist)
  751.               )
  752.            )
  753.          (SETF (DOCUMENTATION ',name 'STRUCTURE) ,docstring)
  754.          ,(if print-function-option
  755.             `(%PUT ',name 'STRUCTURE-PRINT ,print-function-option)
  756.             `(REMPROP ',name 'STRUCTURE-PRINT)
  757.           )
  758.          ',name
  759.      ) )
  760. ) )
  761.  
  762.